package com.banciyuan.bcywebview.biz.write.photoselecotor.ui;
/**
 * @author Aizaz AZ
 */

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.write.photoselecotor.model.PhotoModel;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.AnimationUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class BasePhotoPreviewActivity extends BaseActivity implements OnPageChangeListener, OnClickListener {

    private ViewPager mViewPager;
    private RelativeLayout layoutTop;
    private ImageButton btnBack;
    private TextView tvPercent;
    protected List<PhotoModel> photos;
    protected int current;
    private ImageView ivPhotoSelected;
    protected ArrayList<PhotoModel> selectedPhotos = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photopreview);
        layoutTop = (RelativeLayout) findViewById(R.id.layout_top_app);
        btnBack = (ImageButton) findViewById(R.id.btn_back_app);
        tvPercent = (TextView) findViewById(R.id.tv_percent_app);
        mViewPager = (ViewPager) findViewById(R.id.vp_base_app);

        btnBack.setOnClickListener(this);
        mViewPager.addOnPageChangeListener(this);

        ivPhotoSelected = (ImageView) findViewById(R.id.cb_photo_lpsi);
        ivPhotoSelected.setOnClickListener(this);

        overridePendingTransition(R.anim.activity_alpha_action_in, 0); // 渐入效果

    }

    /**
     * 绑定数据，更新界面
     */
    protected void bindData() {
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(current);
        boolean isChecked = photos.get(current).isChecked();
        if (isChecked) {
            ivPhotoSelected.setImageResource(R.drawable.ic_checkbox_pressed);
        } else {
            ivPhotoSelected.setImageResource(R.drawable.ic_checkbox_normal);
        }
    }

    private PagerAdapter mPagerAdapter = new PagerAdapter() {

        @Override
        public int getCount() {
            if (photos == null) {
                return 0;
            } else {
                return photos.size();
            }
        }

        @Override
        public View instantiateItem(final ViewGroup container, final int position) {
            PhotoPreview photoPreview = new PhotoPreview(getApplicationContext());
            container.addView(photoPreview);
            photoPreview.loadImage(photos.get(position));
            photoPreview.setOnClickListener(photoItemClickListener);
            return photoPreview;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    };
    protected boolean isUp;

    @Override
    public void onBackPressed() {
        Intent data = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("photos", selectedPhotos);
        data.putExtras(bundle);
        setResult(RESULT_OK, data);
        finish();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_back_app) {
            Intent data = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("photos", selectedPhotos);
            data.putExtras(bundle);
            setResult(RESULT_OK, data);
            finish();
        }
        if (v.getId() == R.id.cb_photo_lpsi) {
            boolean checked = false;
            if (photos != null) {
                checked = photos.get(current).isChecked();
            } else {
                return;
            }
            if (!checked) {
                if (selectedPhotos != null && selectedPhotos.size() >= 9) {
                    MyToast.show(this, getString(R.string.max_img_limit_reached));
                } else if (over10m(photos.get(current).getOriginalPath())) {
                    MyToast.show(this, getString(R.string.over_10m));
                } else if (overSize(photos.get(current).getOriginalPath())) {
                    MyToast.show(this, getString(R.string.over_18000));
                } else {
                    photos.get(current).setChecked(true);
                    ivPhotoSelected.setImageResource(R.drawable.ic_checkbox_pressed);
                    selectedPhotos.add(photos.get(current));
                }
            } else {
                photos.get(current).setChecked(false);
                ivPhotoSelected.setImageResource(R.drawable.ic_checkbox_normal);
                selectedPhotos.remove(photos.get(current));
            }
        }
    }

    private boolean over10m(String srcPath) {
        File file = new File(srcPath);
        return file.exists() && file.length() > 10 * 1024 * 1024;
    }

    private boolean overSize(String srcPath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(srcPath, options);
        int w = options.outWidth;
        int h = options.outHeight;
        if (w >= 18000 || h >= 18000) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int arg0) {
        current = arg0;
        updatePercent();
    }

    protected void updatePercent() {
        tvPercent.setText((current + 1) + "/" + photos.size());
        if (current < photos.size() && current >= 0) {
            boolean isChecked = photos.get(current).isChecked();
            if (isChecked) {
                ivPhotoSelected.setImageResource(R.drawable.ic_checkbox_pressed);
            } else {
                ivPhotoSelected.setImageResource(R.drawable.ic_checkbox_normal);
            }
        }
    }

    /**
     * 图片点击事件回调
     */
    private OnClickListener photoItemClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!isUp) {
                new AnimationUtil(getApplicationContext(), R.anim.translate_up)
                        .setInterpolator(new LinearInterpolator()).setFillAfter(true).startAnimation(layoutTop);
                isUp = true;
            } else {
                new AnimationUtil(getApplicationContext(), R.anim.translate_down_current)
                        .setInterpolator(new LinearInterpolator()).setFillAfter(true).startAnimation(layoutTop);
                isUp = false;
            }
        }
    };
}
